﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AresCustomYacht.Code;
using AresCustomYacht.Data;

namespace AresCustomYacht
{
    public partial class Boat : System.Web.UI.Page
    {
        readonly BL _bl = new BL();
        private int _mode = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack) return; //do not load if it is a post back. just fire the events.

            _mode = Utility.GetPageMode(this);
            Utility.ApplyMode((PageMode)_mode, btnAdd,btnUpdate , btnDelete);
            //load the boats into the list box
            using (var dc = new DBDataContext(Constants.ConnectionString))
            {
                var users = dc.aspnet_Users.Select(p => new {p.UserName, p.UserId}).ToList();
                dfcOwner.DataSource = users;
                dfcOwner.DataTextField = "UserName";
                dfcOwner.DataValueField = "UserId";
                dfcOwner.DataBind();
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            _bl.AddBoat(dfsBoatName.Text, new Guid());
            
            Context.Cache.Remove("Boats");
            Context.Cache.Insert("Boats", _bl.GetBoats());
            var linkAddress = Utility.Encrypt("Mode=0&Message=Boat Added");
            Response.Redirect("Boats.aspx?k=" + linkAddress); 

        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {

        }
    }
}